// cgi.h: interface for the CLCGI class.
// $Id: cgi.h,v 1.8 2008/08/13 02:16:27 leon Exp $
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_CGI_H__41592039_77C6_4B7F_9879_D769B4D3CA5F__INCLUDED_)
#define AFX_CGI_H__41592039_77C6_4B7F_9879_D769B4D3CA5F__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "str.h"
#include <stdio.h>

#define 	CLCGI_MAX_POST_SIZE		5*1024*1024

class CLCGI  
{
	CLStrTree	m_form;
	CLStrTree	m_cookie;
public:
	int m_header;					//to indicate header has print before?
	CLStr content_type;
	void outbody(LPCSTR src, int esc=0);
	void outhtml(CLStr& src, int esc=0);
	CLStr url(LPCSTR src);
	static CLStr		default_val;
	static CLStrList 	default_val_list;
	
	static LPCSTR http,https;
	static LPCSTR default_content_type;
	static LPCSTR long_time_expires;
	
	int init(LPCSTR path=0, int maxsize = CLCGI_MAX_POST_SIZE);
	CLCGI(LPCSTR path=0, int maxsize = CLCGI_MAX_POST_SIZE):m_header(0) { init(path, maxsize); }
	~CLCGI() {} 

	void write(int val,int esc=0) {	print_header(); printf("%d",val); }
	void write(unsigned long val,int esc=0) {	print_header(); printf("%u",val); }
	void write(LPCSTR s,int esc=0);
	void write(const CLStr& str,int esc=0) { write((LPCSTR)str,esc); }

	void redirect(CLStr link);
	void location(CLStr link) { printf("Location: %s\n\n",(LPCSTR)link); }
	
	LPCSTR check(const CLStrList& l,const CLStr& val,LPCSTR def="checked");
	LPCSTR check(const CLStr& list,const CLStr& val,LPCSTR def="checked");
	LPCSTR select(const CLStrList& list,LPCSTR val) { return check(list,val,"selected"); }
	LPCSTR select(const CLStr& str,LPCSTR val) { return check(str,val,"selected"); }

	int param(LPCSTR key,int min,int max,int def);
	CLStr param(LPCSTR key,LPCSTR def="");
	CLStr paramlower(LPCSTR key,LPCSTR def="");
	CLStr paramtrim(LPCSTR key,LPCSTR def="");
	CLStr paramtrimlower(LPCSTR key,LPCSTR def="");
	CLStr cookie(LPCSTR key,LPCSTR def="");
	const CLStrList& paramlist(LPCSTR key);
	CLStr paramlist_str(LPCSTR key,LPCSTR sep=",");

	void print_header();
	void location(LPCSTR location);
	void refresh_location(LPCSTR location);
	void js_location(LPCSTR location);
	static void set_cookie(LPCSTR name,LPCSTR value,LPCSTR path="",LPCSTR domain="",LPCSTR expire="");
	static void set_header(LPCSTR header, LPCSTR val);
	void set_content_type(LPCSTR c);
	static int a2i(LPCSTR a,int min,int max,int def);
};

#endif // !defined(AFX_CGI_H__41592039_77C6_4B7F_9879_D769B4D3CA5F__INCLUDED_)
